import com.sun.org.apache.bcel.internal.generic.NEW;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Created whit IntelliJ IDEA.
 * Description：
 * User：ZHONGCHEN
 * Date:2022-02-23
 * Time:10:37
 */
public class TestDemo {
    /**
     * 917. 仅仅反转字母
     * 给你一个字符串 s ，根据下述规则反转字符串：
     * 所有非英文字母保留在原有位置。
     * 所有英文字母（小写或大写）位置反转。
     * <p>
     * 示例 1：
     * <p>
     * 输入：s = "ab-cd"
     * 输出："dc-ba"
     */
    public static void main1(String[] args) {
        String s = "7_28]";
        System.out.println(reverseOnlyLetters(s));
    }

    //双指针
    public static String reverseOnlyLetters(String s) {
        char[] c = s.toCharArray();
        int start = 0;
        int end = c.length - 1;
        while (start < end) {
            //跳过不是大小写字母的
            while (start <= c.length - 1 && ((c[start] < 'a' && c[start] > 'Z') || c[start] > 'z' || c[start] < 'A')) {
                start++;
            }
            while (end >= 0 && ((c[end] < 'a' && c[end] > 'Z') || c[end] > 'z' || c[end] < 'A')) {
                end--;
            }
            //应对全不是大小写字母的情况
            if (start < end) {
                char tmp = c[start];
                c[start] = c[end];
                c[end] = tmp;
                start++;
                end--;
            }
        }
        return String.valueOf(c);
    }

    //更简洁的写法
    public String reverseOnlyLetters1(String s) {
        char[] cs = s.toCharArray();
        int n = cs.length;
        for (int i = 0, j = n - 1; i < j; ) {
            while (i < j && !Character.isLetter(cs[i])) i++;
            while (i < j && !Character.isLetter(cs[j])) j--;
            if (i < j) {
                char c = cs[i];
                cs[i++] = cs[j];
                cs[j--] = c;
            }
        }
        return String.valueOf(cs);
    }

    public static void main3(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        System.out.println(Findlongestnum(s));
    }
    //找最长的数字串
    public static String Findlongestnum(String s) {
        char[] c = s.toCharArray();
        ArrayList<Character> arrayList1 = new ArrayList<>();
        ArrayList<Character> ans = new ArrayList<>();
        int i = 0;
        while (i < c.length) {
            while (i < c.length && c[i] >= '0' && c[i] <= '9') {
                arrayList1.add(c[i]);
                i++;
            }
            if (arrayList1.size() > ans.size()) {
                ans = new ArrayList<>();
                ans.addAll(arrayList1);
                arrayList1 = new ArrayList<>();
            } else {
                arrayList1 = new ArrayList<>();
            }
            i++;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (char cc : ans) {
            stringBuilder.append(cc);
        }
        return String.valueOf(stringBuilder);
    }

    public static void main4(String[] args) {
        int[] num = {1};
        System.out.println(MoreThanHalfNum_Solution(num));
    }

    //找超过数组长度一半的数字
    public static int MoreThanHalfNum_Solution(int[] array) {
        int n = array.length / 2;
        int max = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int a : array) {
            if (map.containsKey(a)) {
                map.put(a, map.get(a) + 1);
                if (map.get(a) > n) {
                    max = a;
                }
            } else {
                map.put(a, 1);
            }
        }
        if (n == 0) {
            return array[0];
        }
        return max;
    }
}
